import { _decorator, Color, Component, error, instantiate, JsonAsset, Label, Node, Prefab, resources, UITransform, Vec3 } from 'cc';
import { NormalCellController } from '../cell/NormalCellController';
import { StartCellController } from '../cell/StartCellController';
import { DiamondCellController } from '../cell/DiamondCellController';
import { FootprintCellController } from '../cell/FootprintCellController';
import { GoldCellController } from '../cell/GoldCellController';
import { MonsterCellController } from '../cell/MonsterCellController';
import { ChestCellController } from '../cell/ChestCellController';
import { ThrowDiceController } from '../ThrowDiceController';
import { TaskController } from '../task/TaskController';
import { FootprintTaskController } from '../task/FootprintTaskController';
const { ccclass, property } = _decorator;

export const CELL_TYPE = {
    START: {
        number: 0,
        path: "prefab/cell/start-cell/StartCell"
    },
    DIAMOND: {
        number: 1,
        path: "prefab/cell/diamond-cell/DiamondCell"
    },
    GOLD: {
        number: 2,
        path: "prefab/cell/gold-cell/GoldCell"
    },
    FOOTPRINT: {
        number: 3,
        path: "prefab/cell/footprint-cell/FootprintCell"
    },
    MONSTER: {
        number: 4,
        path: "prefab/cell/monster-cell/MonsterCell"
    },
    CHEST: {
        number: 5,
        path: "prefab/cell/chest-cell/ChestCell"
    }
}

export const TASK_TYPE = {
    FOOTPRINT: {
        number: 1,
        path: "prefab/task/footprint-task/FootprintTask"
    }
}

@ccclass('RebelDinosaurController')
export class RebelDinosaurController extends Component {

    @property({ type: ThrowDiceController })
    private dice: ThrowDiceController | null = null;

    protected cellControllerMap: Map<number, NormalCellController> = new Map();

    start() {

    }

    update(deltaTime: number) {

    }

    loadMap() {
        // this.node.removeAllChildren()
        resources.load('map/info', (err: any, res: JsonAsset) => {
            this.generateMap(res.json)
        })
    }

    generateMap(jsonData: Object) {
        this.generateCell(jsonData['cellInfo'] as Array<Object>);
        this.generateTask(jsonData['taskInfo'] as Array<Object>);
    }

    private generateCell(jsonData: Array<Object>) {
        let currentNode = this.node.getChildByName("Map")
        jsonData.forEach((value, index, array) => {
            let cellController: NormalCellController = null;
            switch (value['type']) {
                case CELL_TYPE.START.number:
                    cellController = new StartCellController(currentNode, this.dice, CELL_TYPE.START.number)
                        .loadPrefabCell(CELL_TYPE.START.path, value);
                    break;
                case CELL_TYPE.DIAMOND.number:
                    cellController = new DiamondCellController(currentNode, this.dice, CELL_TYPE.DIAMOND.number)
                        .loadPrefabCell(CELL_TYPE.DIAMOND.path, value);
                    break;
                case CELL_TYPE.GOLD.number:
                    cellController = new GoldCellController(currentNode, this.dice, CELL_TYPE.GOLD.number)
                        .loadPrefabCell(CELL_TYPE.GOLD.path, value);
                    break;
                case CELL_TYPE.FOOTPRINT.number:
                    cellController = new FootprintCellController(currentNode, this.dice, CELL_TYPE.FOOTPRINT.number)
                        .loadPrefabCell(CELL_TYPE.FOOTPRINT.path, value);
                    break;
                case CELL_TYPE.MONSTER.number:
                    cellController = new MonsterCellController(currentNode, this.dice, CELL_TYPE.MONSTER.number)
                        .loadPrefabCell(CELL_TYPE.MONSTER.path, value);
                    break;
                case CELL_TYPE.CHEST.number:
                    cellController = new ChestCellController(currentNode, this.dice, CELL_TYPE.CHEST.number)
                        .loadPrefabCell(CELL_TYPE.CHEST.path, value);
                    break;
                default:
                    throw ('未定义砖块')
            }
            this.cellControllerMap.set(cellController.getId(), cellController);
        })

        this.cellControllerMap.forEach((value, index, array) => {
            for (let link of value.getLinkId()) {
                value.addCellControllerArray(this.cellControllerMap.get(link));
            }
        })
    }

    private generateTask(jsonData: Array<Object>) {
        let currentNode = this.node.getChildByName("Task")
        jsonData.forEach((value, index, array) => {
            switch (value['type']) {
                case TASK_TYPE.FOOTPRINT.number:
                    new FootprintTaskController(currentNode, TASK_TYPE.FOOTPRINT.number)
                        .loadPrefabCell(TASK_TYPE.FOOTPRINT.path, value);
                    break;
            }
        })
    }
}
